package com.zeyi.chat.handler;

import com.alibaba.fastjson.JSON;
import com.zeyi.chat.annotation.Mapping;
import com.zeyi.chat.dto.Chat;
import com.zeyi.chat.dto.Message;
import com.zeyi.chat.manager.SocketManager;
import com.zeyi.chat.po.ChatGroup;
import com.zeyi.chat.po.User;
import com.zeyi.chat.service.GroupService;
import com.zeyi.chat.service.UserService;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zeyi
 */
public class AddHandler {

    private AddHandler() {
    }

    private static class Singleton {
        public static final AddHandler INSTANCE = new AddHandler();
    }

    public static AddHandler getInstance() {
        return AddHandler.Singleton.INSTANCE;
    }

    /**
     * 处理好友添加
     */
    @Mapping("addFriend")
    public Message handleFriend(Message message) {
        User sender = message.getSender();
        Integer friendId = Integer.valueOf(message.getData());
        User friend = UserService.getInstance().addFriend(sender.getId(), friendId);
        if (friend != null) {
            TaskHandler.submit(() -> {
                Chat chat = new Chat(sender.getId(), sender.getUsername(), sender.getAvatar(), null, sender.getEmail(),
                        SocketManager.getSocket(friendId) != null);
                Map<String, String> map = new HashMap<>();
                map.put("type", "1");
                map.put("friend", JSON.toJSONString(chat));
                Message message1 = new Message(true, message.getHandlePath(), JSON.toJSONString(map));
                SocketManager.send(SocketManager.getSocket(friendId), message1);
            });
            Chat chat2 = new Chat(friendId, friend.getUsername(), friend.getAvatar(), null, sender.getEmail(),
                    SocketManager.getSocket(friendId) != null);
            Map<String, String> map = new HashMap<>();
            map.put("type", "0");
            map.put("friend", JSON.toJSONString(chat2));
            return new Message(true, message.getHandlePath(), JSON.toJSONString(map));
        } else {
            return new Message(false, message.getHandlePath(), "好友已存在");
        }
    }

    /**
     * 处理加群
     */
    @Mapping("joinGroup")
    public Message handleGroupJoin(Message message) {
        User sender = message.getSender();
        Integer groupId = Integer.valueOf(message.getData());
        ChatGroup group = GroupService.getInstance().joinGroup(sender.getId(), groupId);
        if (group != null) {
            return new Message(true, message.getHandlePath(), JSON.toJSONString(group));
        } else {
            return new Message(false, message.getHandlePath(), "你已经在群里面");
        }
    }

    /**
     * 处理建群
     */
    @Mapping("createGroup")
    public Message handleGroupCreate(Message message) {
        ChatGroup group = JSON.parseObject(message.getData(), ChatGroup.class);
        group.setGroupCode(RandomStringUtils.randomNumeric(12));
        group.setCreateTime(new Date());
        GroupService.getInstance().createGroup(group);
        return new Message(true, message.getHandlePath(), JSON.toJSONString(group));
    }
}
